package com.compus_help.controller.admin;

import com.compus_help.common.PageResult;
import com.compus_help.common.Result;
import com.compus_help.entity.DTO.OrderCategoryDTO;
import com.compus_help.entity.DTO.OrderCategoryPageDTO;
import com.compus_help.service.impl.adminOrderCategoryServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class adminOrderCategoryController {
    private static final Logger log = LoggerFactory.getLogger(adminOrderController.class);

    @Autowired
    private adminOrderCategoryServiceImpl adminOrderCategoryService;
    /**
     * 订单分类分页查询
     * @param orderCategoryPageDTO
     * @return
     */
    @GetMapping("/category/page")
    public Result<PageResult> getOrderCategoryPage(OrderCategoryPageDTO orderCategoryPageDTO) {
        try {
            PageResult pageResult = adminOrderCategoryService.getOrderCategoryPage(orderCategoryPageDTO);
            log.info("订单分类分页查询结果提交: {}", pageResult);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("订单分类分页查询失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新增分类
     * @param orderCategoryPageDTO
     * @return
     */
    @PostMapping("/addcategory")
    public Result addCategory(@RequestBody OrderCategoryPageDTO orderCategoryPageDTO) {
        try {
            log.info("新增分类: {}", orderCategoryPageDTO);
            adminOrderCategoryService.addCategory(orderCategoryPageDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("新增分类失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 状态变更
     * @param status
     * @param categoryId
     * @return
     */
    @PostMapping("category/status/{status}")
    public Result changeStatus(@PathVariable String status, @RequestParam("category_id") Integer categoryId) {
        try {
            adminOrderCategoryService.changeStatus(status, categoryId);
            return Result.success();
        } catch (Exception e) {
            log.error("状态变更失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 编辑分类
     * @param OrderCategoryDTO
     * @return
     */
    @PutMapping("/updatecategory")
    public Result updateCategory(@RequestBody OrderCategoryDTO OrderCategoryDTO) {
        try {
            log.info("编辑分类: {}", OrderCategoryDTO);
            adminOrderCategoryService.updateCategory(OrderCategoryDTO);
            log.info("快递名称: {}", OrderCategoryDTO.getCategoryName());
            return Result.success();
        } catch (Exception e) {
            log.error("编辑分类失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除分类
     * @param categoryId
     * @return
     */
    @DeleteMapping("/deletecategory")
    public Result deleteCategory(@RequestParam("category_id") Integer categoryId) {
        try {
            log.info("删除分类: {}", categoryId);
            adminOrderCategoryService.deleteCategory(categoryId);
            return Result.success();
        } catch (Exception e) {
            log.error("删除分类失败", e);
            return Result.error(e.getMessage());
        }
    }

}
